﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SlimDX;
using SlimDX.Windows;
using SlimDX.DXGI;
using SlimDX.Direct3D11;
using Device = SlimDX.Direct3D11.Device;
using Resource = SlimDX.Direct3D11.Resource;
using System.Diagnostics;
using SlimDX.D3DCompiler;

namespace LabyrinthGame
{
    public partial class MainWindow : RenderForm
    {
        /*-----------*
         * Variables *
         *-----------*/
        private Size windowSize480 = new Size(640, 480);
        private Size windowSize720 = new Size(1280, 720);
        private Size windowSize900 = new Size(1600, 900);

        private long curretFrameTime = 0;
        private long lastFrameTime = 0;
        public int fps { get; private set; }
        private float clientSizeMod = 0;

        private Viewport2D viewport;
        private Color4 backColor = new Color4(0.5f, 0.5f, 1.0f);

        //testing fields
        private Cell[,] cells = new Cell[8, 8];
        private Cell cell;
        int x = 0;
        int y = 0;

        /*--------------*
         * Constructors *
         *--------------*/

        public MainWindow(Color4 color)
        {
            this.FormClosed += MainWindow_FormClosed;
            this.Load += MainWindow_Load;
            this.ClientSizeChanged += MainWindow_ClientSizeChanged;
            if (backColor != null)
                this.backColor = color;

            InitializeComponent();
        }

        /*------------------*
         * Events handlers  *
         *------------------*/

        void MainWindow_ClientSizeChanged(object sender, EventArgs e)
        {
            clientSizeMod = (float)this.Height / (float)this.Width;
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            this.Show();
            this.Size = windowSize900;
            this.MaximumSize = windowSize900;
            this.MinimumSize = windowSize900;
            clientSizeMod = (float)this.Height / (float)this.Width;

            viewport = new Viewport2D(this, new SwapChainDescription()
            {
                BufferCount = 2,
                Usage = Usage.RenderTargetOutput,
                OutputHandle = this.Handle,
                IsWindowed = true,
                ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags = SwapChainFlags.AllowModeSwitch,
                SwapEffect = SwapEffect.Discard
            });

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    cells[i, j] = new Cell(i - 4, j - 4, 0, clientSizeMod, false, viewport);
                }
            }
            cell = new Cell(-4, -4, 0, clientSizeMod, true, viewport);
        }

        private void MainWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            viewport.Dispose();
            Environment.Exit(0);
        }

        /*----------------------*
         * My methods for game  *
         *----------------------*/
        public void startGameLoop()
        {
            MessagePump.Run(this, gameLoop);
        }
        public void gameLoop()
        {
            lastFrameTime = curretFrameTime;
            curretFrameTime = Stopwatch.GetTimestamp();

            updateGameState((double)(curretFrameTime - lastFrameTime) / Stopwatch.Frequency);

            renderScene();

            fps = (int)(Stopwatch.Frequency / (float)(curretFrameTime - lastFrameTime));
        }
        public void updateGameState(double frameTime)
        {
            //x = cell.x;
            //y = cell.y;
            //if (x < 3)
            //{
            //    x++;
            //}
            //else
            //{
            //    if (y < 3)
            //    {
            //        y++;
            //        x = -4;
            //    }
            //    else
            //    {
            //        x = -4;
            //        y = -4;
            //    }
            //}
            //cell.moveTo(x, y);
        }
        public void drawFloor()
        {

        }
        public void renderScene()
        {
            viewport.context.ClearRenderTargetView(viewport.renderTarget, backColor);
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    cells[i, j].draw();
                }
            }
            cell.draw();
            viewport.swapChain.Present(0, PresentFlags.None);
        }
    }
}
